<template>
  <div>
    <el-card style="margin-bottom: 20px">
      <CategorySelect
        @getCategoryList="getCategoryList"
        :show="!isShowTable"
      ></CategorySelect>
    </el-card>
    <el-card>
      <div v-show="isShowTable">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin:10px 0px;0px"
          :disabled="!category3Id"
          @click="addProperty"
          >添加属性</el-button
        >
        <el-table :data="attrList" style="width: 100%">
          <el-table-column
            prop="prop"
            label="序号"
            type="index"
            align="center"
            width="100"
          >
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="150">
          </el-table-column>
          <el-table-column prop="prop" label="属性列表值" width="width">
            <template slot-scope="{ row }">
              <el-tag
                type="success"
                v-for="att in row.attrValueList"
                :key="att.id"
                style="margin-left: 10px"
                >{{ att.valueName }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="200">
            <template slot-scope="{ row }">
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="updateAttr(row)"
                >修改</el-button
              >
              <el-popconfirm
                :title="`确定删除${row.attrName}吗?`"
                @onConfirm="deleteAttr(row)"
              >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  slot="reference"
                  >删除</el-button
                ></el-popconfirm
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div v-show="!isShowTable">
        <!-- 收集属性名 -->
        <el-form ref="form" :model="attrInfo">
          <el-form-item label="属性名">
            <el-input
              placeholder="请输入属性名"
              style="width: 20%"
              v-model="attrInfo.attrName"
            ></el-input>
          </el-form-item>
        </el-form>

        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin:10px 0px;0px"
          :disabled="!attrInfo.attrName"
          @click="addAttr"
          >添加属性值</el-button
        >
        <el-button type="warning" @click="isShowTable = true">取消</el-button>

        <!-- 收集属性值 -->
        <el-table
          style="width: 100%; margin: 20px 0px"
          border="border"
          :data="attrInfo.attrValueList"
        >
          <el-table-column
            prop="prop"
            label="序号"
            align="center"
            type="index"
            width="100"
          >
          </el-table-column>
          <el-table-column prop="prop" label="属性值名称" width="width">
            <template slot-scope="{ row }">
              <el-input
                v-model="row.valueName"
                placeholder="请输入属性值名称"
                size="mini"
                v-if="row.flag"
                ref="ipt"
                @blur="toLook(row)"
                @keyup.native.enter="toLook(row)"
              ></el-input>
              <span
                style="display: block"
                v-else
                @dblclick="changeInput(row)"
                >{{ row.valueName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-popconfirm
                :title="`确定删除${row.valueName}吗?`"
                @onConfirm="onConfirm($index)"
              >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  slot="reference"
                >
                </el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-button type="primary" @click="addOrUpdateAttr">保存</el-button>
        <el-button type="warning" @click="isShowTable = true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>

export default {
  name: "Attr",
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      attrList: [],

      //控制表格显示与隐藏
      isShowTable: true,

      //收集属性信息
      attrInfo: {
        attrName: "",
        attrValueList: [],
        categoryId: 0, //三级分类id
        categoryLevel: 3, //服务器需要几级id
      },
    };
  },
  methods: {
    getCategoryList(data) {
      let { categoryId, level } = data;

      if (level == 1) {
        this.category2Id = "";
        this.category3Id = "";
        this.category1Id = categoryId;
      } else if (level == 2) {
        this.category3Id = "";
        this.category2Id = categoryId;
      } else if (level == 3) {
        this.category3Id = categoryId;
        // 发送请求获取数据展示
        this.getAttrList();
      }
    },

    async getAttrList() {
      const { category1Id, category2Id, category3Id } = this;
      let result = await this.$API.attr.reqAttrList(
        category1Id,
        category2Id,
        category3Id
      );
      if (result.code == 200) {
        this.attrList = result.data;
      }
    },

    addAttr() {
      this.attrInfo.attrValueList.push({
        attrId: this.attrInfo.id, //属性名的id
        valueName: "",
        flag: true, //给每一个属性添加一个标记是否为查看模式
      });
      this.$nextTick(() => {
        this.$refs.ipt.focus();
      });
    },

    // 添加属性按钮回调
    addProperty() {
      this.isShowTable = !this.isShowTable;
      this.attrInfo = {
        attrName: "",
        attrValueList: [],
        categoryId: this.category3Id, //三级分类id
        categoryLevel: 3, //服务器需要几级id
      };
    },

    //修改某个属性
    updateAttr(row) {
      this.isShowTable = false;
      //深拷贝row里面内容，赋值一份进行展示

      let copyRow = JSON.stringify(row);
      this.attrInfo = JSON.parse(copyRow);
      this.attrInfo.attrValueList.forEach((item) => {
        this.$set(item, "flag", false);
      });
    },

    toLook(row) {
      if (row.valueName.trim() == "") {
        this.$message({
          type: "warning",
          message: "输入属性值不能为空",
        });
        return;
      }

      let isReapet = this.attrInfo.attrValueList.some((item) => {
        if (row != item) {
          return row.valueName == item.valueName;
        }
      });

      if (isReapet) {
        this.$message({
          type: "warning",
          message: "输入属性值不能重复",
        });
        row.valueName = "";
        return;
      }
      row.flag = !row.flag;
    },
    changeInput(row) {
      row.flag = !row.flag;
      this.$nextTick(() => {
        this.$refs.ipt.focus();
      });
    },

    onConfirm(index) {
      this.attrInfo.attrValueList.splice(index, 1);
    },

    // 点击保存按钮发送请求
    async addOrUpdateAttr() {
      console.log(111);
      //整理请求参数 1.如果用户添加的属性值为空不应该提交给服务器，flag字段也不应该给服务器添加
      this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter(
        (item) => {
          if (item.valueName != "") {
            delete item.flag;
            return true;
          }
        }
      );
      console.log(this.attrInfo);
      //发送请求
      try {
        await this.$API.attr.reqAddOrUpdateAttr(this.attrInfo);
        this.$message({
          type: "success",
          message: "保存成功",
        });
        this.isShowTable = true;
        this.getAttrList();
      } catch (e) {}
    },

    // 删除某个属性
    async deleteAttr(row) {
      console.log(row);
      try {
        await this.$API.attr.reqDeleteAttr(row.id);
        this.$message({
          type: "success",
          message: "删除成功",
        });
        this.getAttrList();
      } catch (error) {
        this.$message({
          type: "error",
          message: error.message,
        });
      }
    },
  },
};
</script>

<style>
</style>